package com.egg.ylt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.fragment.FRA_OrderItem;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ScreenUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by qiuzhenhuan on 2019/1/21.
 */

public class ACT_Order extends BaseActivity {

    @BindView(R.id.title_rl)
    RelativeLayout mTitleRl;
    @BindView(R.id.order_category_tb)
    TabLayout mOrderCategoryTb;
    @BindView(R.id.order_ll)
    LinearLayout mOrderLl;
    @BindView(R.id.no_login_ll)
    LinearLayout mNoLoginLl;

    private FRA_OrderItem mOrderAllFragment;
    private FRA_OrderItem mOrderReservedFragment;
    private FRA_OrderItem mOrderUnderwayFragment;
    private FRA_OrderItem mOrderAccomplishFragment;

    //fragment对象
    private Fragment mCurrentFragment;

    private String[] mOrderCategoryTitles = new String[]{"全部", "已预约", "进行中", "已完成"};

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mTitleRl);

        getTokenInitView();

        //初始化fragment
        mOrderAllFragment = FRA_OrderItem.newInstance(3);
        mOrderReservedFragment = FRA_OrderItem.newInstance(0);
        mOrderUnderwayFragment = FRA_OrderItem.newInstance(1);
        mOrderAccomplishFragment = FRA_OrderItem.newInstance(2);

        // 设置主页显示的页面
        if (!mOrderAllFragment.isAdded()) {
            mCurrentFragment = mOrderAllFragment;
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_container, mOrderAllFragment);
            transaction.commitAllowingStateLoss();
        }

        //tableLayout相关
        for (int i = 0; i < mOrderCategoryTitles.length; i++) {
            mOrderCategoryTb.addTab(mOrderCategoryTb.newTab());
            mOrderCategoryTb.getTabAt(i).setCustomView(makeTabView(i));
        }

        mOrderCategoryTb.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                assert customView != null;
                customView.findViewById(R.id.indicator_view).setVisibility(View.VISIBLE);
                TextView titleTv = customView.findViewById(R.id.order_category_tv);
                titleTv.setTextColor(getResources().getColor(R.color.color_333333));
                String title = titleTv.getText().toString();
                int position = -1;
                for (int i = 0; i < mOrderCategoryTitles.length; i++) {
                    if (title.equals(mOrderCategoryTitles[i])) {
                        position = i;
                        break;
                    }
                }
                switch (position) {
                    case 0:
                        switchFragmentContent(mCurrentFragment, mOrderAllFragment);
                        break;
                    case 1:
                        switchFragmentContent(mCurrentFragment, mOrderReservedFragment);
                        break;
                    case 2:
                        switchFragmentContent(mCurrentFragment, mOrderUnderwayFragment);
                        break;
                    case 3:
                        switchFragmentContent(mCurrentFragment, mOrderAccomplishFragment);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                customView.findViewById(R.id.indicator_view).setVisibility(View.INVISIBLE);
                TextView titleTv = customView.findViewById(R.id.order_category_tv);
                titleTv.setTextColor(getResources().getColor(R.color.color_666666));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    /**
     * 创建tabLayout子item布局
     *
     * @param position
     * @return
     */
    private View makeTabView(int position) {
        View tabView = LayoutInflater.from(this).inflate(R.layout.tab_text_icon, null);
        TextView textView = tabView.findViewById(R.id.order_category_tv);
        textView.setText(mOrderCategoryTitles[position]);
        //显示第一个指示器
        if (position == 0) {
            tabView.findViewById(R.id.indicator_view).setVisibility(View.VISIBLE);
        } else {
            tabView.findViewById(R.id.indicator_view).setVisibility(View.INVISIBLE);
        }
        return tabView;
    }


    @OnClick({R.id.to_login_tv, R.id.include_iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.to_login_tv://去登录
                readyGo(ACT_UserLogin.class);
                break;
            case R.id.include_iv_back:
                finish();
                break;
        }
    }


    /**
     * 切换Fragment
     *
     * @param fromFragment
     * @param toFragment
     */
    public void switchFragmentContent(Fragment fromFragment, Fragment toFragment) {
        if (mCurrentFragment != toFragment) {
            mCurrentFragment = toFragment;
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (!toFragment.isAdded()) { // 先判断是否被add过
                transaction.hide(fromFragment).add(R.id.fragment_container, toFragment).commitAllowingStateLoss(); //
                // 隐藏当前的fragment，add
                // 下一个到Activity中
            } else {
                transaction.hide(fromFragment).show(toFragment).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
//                refreshData(toFragment);
            }
        }
    }

    /**
     * 根据用户是否登录展示不同view
     *
     * @return
     */
    public void getTokenInitView() {
        if (StringUtil.isEmpty(Constants.TOKEN)) {
            mNoLoginLl.setVisibility(View.VISIBLE);
            mOrderLl.setVisibility(View.GONE);
        } else {
            mOrderLl.setVisibility(View.VISIBLE);
            mNoLoginLl.setVisibility(View.GONE);
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_order;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }


    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    public static void startAc(Context context) {
        context.startActivity(new Intent(context, ACT_Order.class));
    }
}
